<script lang='ts' setup name='Wacth3'>
import { ref, reactive, watch } from 'vue'

const person = reactive({
  name: '张飞',
  age: 18,
  adddress:{
    provice:{
      name:'荆州',
      zcode:'150001'
    }
  }
})



function changeName() {
  person.name += '~';
}

function changeAge() {
  person.age += 1;
}


function changePerson() {
  Object.assign(person, {
    name: '刘备',
    age: 58
  })
}

function changeZcode(){
  person.adddress.provice.zcode ="250001"
}

// watch(想要监听的对象，一旦数据发生变化 时候回调的函数)

watch(person,(newVal,oldVal)=>{
  console.log("person数据发生变化",newVal,oldVal);
  
})



</script>

<template>

  <div class="person">
    <h1>情况三：监视【reactive】定义的【对象类型】数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <h2>深度属性：{{ person.adddress.provice.zcode }}</h2>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
     <button @click="changePerson">修改整个人</button>
     <button @click="changeZcode">修改深度属性</button>
  </div>

</template>

<style scoped></style>
